package models

import "github.com/snail007/go-activerecord/mysql"

const Table_ProjectNode_Name = "project_node"

type ProjectNode struct {
}

var ProjectNodeModel = ProjectNode{}

// DeleteProjectNodeByNodeId: nodeId 删除项目和节点关联关系
func (p *ProjectNode) DeleteProjectNodeByNodeId(nodeId string) (err error) {
	db := G.DB()
	_, err = db.Exec(db.AR().Delete(Table_ProjectNode_Name, map[string]interface{}{
		"node_id": nodeId,
	}))
	return
}

// DeleteByProjectIdNodeIds: projectId 批量删除项目和节点关联关系
func (p *ProjectNode) DeleteByProjectIdNodeIds(projectId string, nodeIds []string) (err error) {
	db := G.DB()
	_, err = db.Exec(db.AR().Delete(Table_ProjectNode_Name, map[string]interface{}{
		"node_id":    nodeIds,
		"project_id": projectId,
	}))
	return
}

// DeleteByProjectId: ProjectId 删除项目和节点关联关系
func (p *ProjectNode) DeleteByProjectId(projectId string) (err error) {
	db := G.DB()
	_, err = db.Exec(db.AR().Delete(Table_ProjectNode_Name, map[string]interface{}{
		"project_id": projectId,
	}))
	return
}

// InsertBatch: 批量插入项目和节点关联关系
func (n *ProjectNode) InsertBatch(insertValues []map[string]interface{}) (id int64, err error) {

	db := G.DB()
	var rs *mysql.ResultSet
	rs, err = db.Exec(db.AR().InsertBatch(Table_ProjectNode_Name, insertValues))
	if err != nil {
		return
	}
	id = rs.LastInsertId
	return
}

// GetProjectNodeByProjectId: projectId 获取项目和节点关联关系
func (p *ProjectNode) GetProjectNodeByProjectId(projectId string) (projectNodes []map[string]string, err error) {
	db := G.DB()
	var rs *mysql.ResultSet
	rs, err = db.Query(db.AR().From(Table_ProjectNode_Name).Where(map[string]interface{}{
		"project_id": projectId,
	}))
	if err != nil {
		return
	}
	projectNodes = rs.Rows()
	return
}

/*
CREATE TABLE `cp_project_node` (
  `project_node_id` int(10) NOT NULL AUTO_INCREMENT COMMENT '项目节点ID',
  `project_id` int(10) NOT NULL DEFAULT '0' COMMENT '项目ID',
  `node_id` int(10) NOT NULL DEFAULT '0' COMMENT '节点ID',
  `create_time` int(11) NOT NULL DEFAULT '0' COMMENT '创建时间',
  PRIMARY KEY (`project_node_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='项目和节点关系表'
*/
